package wymiana.mbeans.department;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import wymiana.domain.Department;
import wymiana.domain.School;
import wymiana.service.DepartmentService;
import wymiana.service.SchoolService;

@ManagedBean(name = "departmentMBean")
// @RequestScoped
@SessionScoped
public class DepartmentMBean implements Serializable {

	/** serialization uid */
	private static final long serialVersionUID = 6414285169668886909L;

	/** lista szkol */
	private List<Department> departments = null;

	private List<Department> filteredDepartments = null;

	/** aktualnie wybrana uczelnia */
	private Department actualDepartment = null;

	@ManagedProperty("#{departmentService}")
	private DepartmentService departmentService;

	@ManagedProperty("#{schoolService}")
	private SchoolService schoolService;

	private String choosenId = null;

	public DepartmentMBean() {
		// TODO inicjalizacja MBeana

	}

	public String goToDepartments() {
		return "department";
	}

	public String editDepartment() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String departmentId = (String) externalContext.getRequestParameterMap()
				.get("departmentId");
		actualDepartment = departmentService.getDepartment(Integer
				.parseInt(departmentId));
		choosenId = actualDepartment.getSchool().getIdSchool().toString();
		return "editDepartment";
	}

	public String addDepartment() {
		actualDepartment = new Department();
		return "addDepartment";
	}

	public String removeDepartment() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String departmentId = (String) externalContext.getRequestParameterMap()
				.get("departmentId");
		// School s = schoolService.getSchool(Integer.parseInt(schoolId));
		setDepartments(null);

		departmentService.deleteDepartmentById(Integer.parseInt(departmentId));
		return "";
	}

	public String showDepartment() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String departmentId = (String) externalContext.getRequestParameterMap()
				.get("departmentId");

		actualDepartment = departmentService.getDepartment(Integer
				.parseInt(departmentId));

		return "showDepartment";
	}


	public String saveDepartment() {

		if (actualDepartment.getIdDepartment() == null) {
			actualDepartment.setCreationDate(new Date());
			School s = schoolService.getSchool(Integer.parseInt(choosenId));
			actualDepartment.setSchool(s);
			this.departmentService.create(actualDepartment);
		} else {
			School s = schoolService.getSchool(Integer.parseInt(choosenId));
			actualDepartment.setSchool(null);
			this.departmentService.updateDepartment(actualDepartment);
			actualDepartment.setSchool(s);
			this.departmentService.updateDepartment(actualDepartment);
		}

		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Sukces: ", "Zapisano zmiany");

		FacesContext.getCurrentInstance().addMessage(null, message);
		boolean contains = false;
		Department toDel = null;
		for (Department d : getDepartments()) {
			if (d.getIdDepartment().equals(actualDepartment.getIdDepartment())) {
				contains = true;
				toDel = d;
				break;
			}
		}

		if (!contains) {
			getDepartments().add(actualDepartment);
			actualDepartment = null;
		} else {
			getDepartments().remove(toDel);
			getDepartments().add(actualDepartment);
		}

		return "department";
	}

	public String saveEditedDepartment() {
		saveDepartment();
		return "";
	}

	// public StreamedContent getImage() {
	// ExternalContext externalContext =
	// FacesContext.getCurrentInstance().getExternalContext();
	//
	// if (FacesContext.getCurrentInstance().getRenderResponse()) {
	// // So, we're rendering the view. Return a stub StreamedContent so
	// // that it will generate right URL.
	// return new DefaultStreamedContent();
	// } else {
	// String imageId = (String)
	// externalContext.getRequestParameterMap().get("imageIdD");
	// Photo p = null;
	// if (actualDepartment.getPhotos() != null) {
	// for (Photo photo : actualDepartment.getPhotos()) {
	// if (photo.getIdPhoto().toString().equals(imageId)) {
	// p = photo;
	// }
	// }
	// }
	// StreamedContent image = null;
	// if (p != null) {
	// image = new DefaultStreamedContent(new
	// ByteArrayInputStream(p.getPhoto()), "image/png");
	//
	// return image;
	// }
	// }
	//
	// return null;
	// }

	// public String deleteActualPhoto() {
	// ExternalContext externalContext =
	// FacesContext.getCurrentInstance().getExternalContext();
	//
	// String imageId = (String)
	// externalContext.getRequestParameterMap().get("imageIdD");
	//
	// photoService.deleteById(Integer.parseInt(imageId));
	// int i = 0;
	// for (Photo p : actualDepartment.getPhotos()) {
	// if (p.getIdPhoto().toString().equals(imageId)) {
	// break;
	// }
	// i++;
	// }
	// actualDepartment.getPhotos().remove(i);
	// FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
	// "Sukces: ", "Usunięto obraz");
	//
	// FacesContext.getCurrentInstance().addMessage(null, message);
	//
	// return "";
	// }

	/**
	 * @return the schools
	 */
	public List<Department> getDepartments() {
		if (departments == null) {
			departments = departmentService.getAllDepartments();
		}
		return departments;
	}

	public Department getActualDepartment() {
		return actualDepartment;
	}

	public void setActualDepartment(Department actualDepartment) {
		this.actualDepartment = actualDepartment;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	/**
	 * @return the choosenId
	 */
	public String getChoosenId() {
		return choosenId;
	}

	/**
	 * @param choosenId
	 *            the choosenId to set
	 */
	public void setChoosenId(String choosenId) {
		this.choosenId = choosenId;
	}

	/**
	 * @return the schoolService
	 */
	public SchoolService getSchoolService() {
		return schoolService;
	}

	/**
	 * @param schoolService
	 *            the schoolService to set
	 */
	public void setSchoolService(SchoolService schoolService) {
		this.schoolService = schoolService;
	}

	/**
	 * @return the filteredDepartments
	 */
	public List<Department> getFilteredDepartments() {
		return filteredDepartments;
	}

	/**
	 * @param filteredDepartments
	 *            the filteredDepartments to set
	 */
	public void setFilteredDepartments(List<Department> filteredDepartments) {
		this.filteredDepartments = filteredDepartments;
	}

}
